<?php
/**
 * Created by PhpStorm.
 * User: zouhua
 * Date: 2017/4/27
 * Time: 17:46
 */

namespace app\widgets;


use app\models\Activity;
use yii\base\Widget;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\widgets\Menu;

class AppMenuWidget extends Widget {

    public $encodeLabels = FALSE;

    public function run() {
        $cidList = Activity::$cidLabels;
        $cidIcon = [
            Activity::CID_SHA_LONG => 'xe63b',
            Activity::CID_SAI_SHI => 'xe724',
            Activity::CID_ZHI_YUAN_ZHE => 'xe649',
            Activity::CID_JOB => 'xe6bd',
            Activity::CID_PRACTICE => 'xe645',
        ];
        $items = [];
        foreach ($cidList as $cid => $name) {
            $icon = $cidIcon[$cid];
            $span = "<span class=\"mui-icon\"><i class=\"iconfont\">&#{$icon};</i></span>";
            $span .= "<span class=\"mui-tab-label\">{$name}</span>";

            $activeClass = $this->isActive($cid) ? ' mui-active' : '';
            $url = Url::to(['activity/', 'cid' => $cid]);
            $items[] = Html::a($span, NULL, ['data-href' => $url, 'class' => "mui-tab-item{$activeClass}"]);
        }

        $html = join("\n", $items);
        return Html::tag('nav', $html, ['class' => 'mui-bar mui-bar-tab']);
    }

    protected function isActive($cid) {
        $params = \Yii::$app->request->queryParams;
        return isset($params['cid']) && $params['cid'] == $cid;
    }

    protected function renderItem($item) {
        if (isset($item['url'])) {
            $template = ArrayHelper::getValue($item, 'template', $this->linkTemplate);

            return strtr($template, [
                '{url}' => Html::encode(Url::to($item['url'])),
                '{label}' => $item['label'],
                '{active}' => $item['active'] ? $this->activeCssClass : ''
            ]);
        } else {
            $template = ArrayHelper::getValue($item, 'template', $this->labelTemplate);

            return strtr($template, [
                '{label}' => $item['label'],
            ]);
        }
    }
}